package com.jackchong.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jackchong.utils.JAuto;
import com.utils.R;

;


/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/14 9:56
 */

public abstract class BaseFragment extends Fragment implements ActivityHelper{

	protected BaseToolsActivity mActivity;
	protected View mRootView;

	public BaseFragment() {}

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		mActivity = (BaseToolsActivity) context;
	}

	/**
	 * 设置子类布局
	 */
	protected abstract int getRootView();

	protected boolean onAutoSize(View view, int id){
		return true;
	}


	public abstract void onGetFocus(boolean switchover);

	/**
	 * 当Fragment失去焦点时
	 * @param fragment
	 */
	public abstract void onLoseFocus(Fragment fragment);

	public abstract void init();


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mRootView = inflater.inflate(getRootView(), null);
		if (onAutoSize(mRootView, mRootView.getId())){
			JAuto.auto(mRootView, this::onAutoSize);
		}
		init();
		return mRootView;
	}



	@Override
	public void onDestroy() {
		super.onDestroy();
	}


	@Override
	public void setWindowFILL(boolean fill) {
		mActivity.setWindowFILL(fill);
	}

	@Override
	public View inflateView(int layout) {
		return mActivity.inflateView(layout);
	}

	@Override
	public View inflateView(int layout, boolean auto) {
		return mActivity.inflateView(layout, auto);
	}

	@Override
	public void openSerivce(Class cls) {
		mActivity.openSerivce(cls);
	}

	@Override
	public void openActivity(Class cls){
		openActivity(new Intent(mActivity, cls), false);
	}
	@Override
	public void openActivity(Class cls, boolean finish){
		openActivity(new Intent(mActivity, cls), finish);
	}
	@Override
	public void openActivity(Intent intent){
		openActivity(intent, null, false);
	}

	@Override
	public void openActivity(Intent intent, boolean finish){
		openActivity(intent,null, finish);
	}

	@Override
	public void openActivity(Intent intent, @Nullable Bundle options, boolean finish){
		mActivity.startActivity(intent, options);
		mActivity.overridePendingTransition(R.anim.activity_show_left, R.anim.activity_show_right);
		if (finish) finishActivity();
	}


	@Override
	public void openActivityForResult(Class cls, int requestCode){
		openActivityForResult(new Intent(mActivity, cls), requestCode);
	}

	@Override
	public void openActivityForResult(Intent intent, int requestCode){
		mActivity.startActivityForResult(intent,requestCode);
		mActivity.overridePendingTransition(R.anim.activity_show_left, R.anim.activity_show_right);
	}

	@Override
	public void finishActivity(){
		hideInputKeyVisibility();
		mActivity.finish();
		mActivity.overridePendingTransition(R.anim.activity_hide_left, R.anim.activity_hide_right);
	}

	@Override
	public int getColorResources(int id) {
		return getResources().getColor(id);
	}

	@Override
	public void hideInputKeyVisibility() {
		mActivity.hideInputKeyVisibility();
	}

	@Override
	public void hideInputKeyVisibility(long delay) {
		mActivity.hideInputKeyVisibility(delay);
	}

	@Override
	public void showInputKeyVisibility(EditText anchor) {
		mActivity.showInputKeyVisibility(anchor);
	}

	@Override
	public boolean getInputKeyVisibility() {
		return 	mActivity.getInputKeyVisibility();
	}

	@Override
	public boolean isScreenOrientation() {
		return 	mActivity.isScreenOrientation();
	}

	@Override
	public void setScreenOrientation(boolean isPortrait) {
		mActivity.setScreenOrientation(isPortrait);
	}

	@Override
	public void onReceiveEvent(EventMgsBean event) {

	}

	@Override
	public final void onSendEvent(EventMgsBean event) {
		mActivity.runOnUiThread(() -> onReceiveEvent(event));
	}
}